/*
 * Copyright 2024 The CHANGLEI Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

part of '../px.dart';

/// 像素大小
extension RSTransformToPx on RSTransform {
  /// 像素大小
  PxRSTransform get px => PxRSTransform(scos.px, ssin.px, tx.px, ty.px);

  /// 像素大小
  PxRSTransform toPx(BuildContext context) {
    return PxRSTransform(
      scos.toPx(context),
      ssin.toPx(context),
      tx.toPx(context),
      ty.toPx(context),
    );
  }
}

/// Created by changlei on 2024/4/23.
///
/// See [PxRSTransform]
class PxRSTransform implements _Px<RSTransform> {
  /// See [RSTransform.new]
  PxRSTransform(PxDouble scos, PxDouble ssin, PxDouble tx, PxDouble ty) {
    _value
      ..[0] = scos._
      ..[1] = ssin._
      ..[2] = tx._
      ..[3] = ty._;
  }

  /// See [RSTransform.fromComponents]
  factory PxRSTransform.fromComponents({
    required PxDouble rotation,
    required PxDouble scale,
    required PxDouble anchorX,
    required PxDouble anchorY,
    required PxDouble translateX,
    required PxDouble translateY,
  }) {
    final scos = cos(rotation) * scale;
    final ssin = sin(rotation) * scale;
    final tx = translateX + -scos * anchorX + ssin * anchorY;
    final ty = translateY + -ssin * anchorX - scos * anchorY;
    return PxRSTransform(scos, ssin, tx, ty);
  }

  final Float32List _value = Float32List(4);

  /// See [RSTransform.scos]
  PxDouble get scos => _value[0].px;

  /// See [RSTransform.ssin]
  PxDouble get ssin => _value[1].px;

  /// See [RSTransform.tx]
  PxDouble get tx => _value[2].px;

  /// See [RSTransform.ty]
  PxDouble get ty => _value[3].px;

  @override
  RSTransform toDp(BuildContext context) {
    return RSTransform(
      scos.toDp(context),
      ssin.toDp(context),
      tx.toDp(context),
      ty.toDp(context),
    );
  }
}
